/**
 * bsw_pageca.h
 * 声明内存片段连续管理类型及接口
 * 
 * 竹影半墙
 * 2023.08
 */
#ifndef BSW_PAGECA_H
#define BSW_PAGECA_H

#include "bsw_memca.h"

typedef struct bsw_pageca bsw_pageca_s;

#if 0
#endif
/**
 * 页式预分配管理
 * @ma    指定大小预分配管理
 * @page  预分配页下一个有效地址
 * @start 预分配页起始地址
 * @size  预分配页大小
 */
struct bsw_pageca {
    bsw_memca_s ma;
    void *page;
    void *start;
    size_t size;
};
#define bsw_pa_to_ma(pa) ((bsw_memca_s*)(pa))

static bsw_always_inline uint16_t bsw_pca_size(bsw_pageca_s *pa)
{
    return pa->ma.size;
}

static bsw_always_inline void 
bsw_pca_app(bsw_pageca_s *pa, uint16_t size, uint32_t max)
{
    bsw_memca_s *ma = bsw_pa_to_ma(pa);

    ma->size = size;
    ma->max = max;
    ma->num = 0;
    ma->head = NULL;
    pa->page = NULL;
    pa->start = NULL;
    pa->size  = size * max;
    return ;
}

void bsw_pca_init (bsw_pageca_s *pa);
void bsw_pca_close(bsw_pageca_s *pa);

void * bsw_pca_alloc(bsw_pageca_s *pa);
void   bsw_pca_free (bsw_pageca_s *pa, void *v);
void * bsw_pca_swap(bsw_pageca_s *pa, void *v);
#endif